package day14.sax;

import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

/**
 * @author oldliu
 * @since 1.0
 */
public class Java79Handler extends DefaultHandler {
    @Override
    public void startDocument() throws SAXException {
        System.out.println("文档开始处理了");
    }

    @Override
    public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
        System.out.println(uri+","+qName+".节点"+localName+"开始了。。。");
        System.out.println("======属性有：");
        for(int i=0;attributes!=null&& i<attributes.getLength();i++){
            System.out.println(attributes.getQName(i) +':'+ attributes.getValue(i));
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        System.out.println(uri+","+qName+".节点"+localName+"处理结束。。。");
    }

    @Override
    public void characters(char[] ch, int start, int length) throws SAXException {

        String str=new String(ch,start,length);
        System.out.println("节点值："+str+",长度："+str.length());
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("文档处理结束了");
    }

    public static void main(String[] args) throws Exception{
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser parser = factory.newSAXParser();
        parser.parse("file:///D:\\Documents\\桌面\\exe\\3-java-advance\\9-xml-作业\\products.xml", new Java79Handler());
    }
}
